#include "GUITextButton.h"

GUITextButton::GUITextButton()
{
    font = ContentManager::LoadContent<Texture2D>("Media/DejaVuSansMono.tga");

    textDispList[Normal] = glGenLists(1);
    textDispList[Hover] = glGenLists(1);
    textDispList[Active] = glGenLists(1);

    // setText(text);
}

void GUITextButton::draw()
{
    /// TODO: Do we want to constrain the text to a set button width/height, or do we want to have the text + padding size
    /// influence the button width/height?
    DrawHelper::Instance().drawRect2D(0, 0, width, height, borderColor[state], true);
    DrawHelper::Instance().drawRect2D(borderSize, borderSize, width-borderSize, height-borderSize, bgColor[state], true);
    //DrawHelper::Instance().drawRect2D(0, 0, width, height, borderColor[state], false);
    glCallList(textDispList[state]);
}

void GUITextButton::setText(std::string newText)
{
    text = newText;

    glNewList(textDispList[Normal], GL_COMPILE);
        DrawHelper::Instance().drawStr2D(text, fontSize, padding, padding, textColor[Normal], font);
    glEndList();
    glNewList(textDispList[Hover], GL_COMPILE);
        DrawHelper::Instance().drawStr2D(text, fontSize, padding, padding, textColor[Hover], font);
    glEndList();
    glNewList(textDispList[Active], GL_COMPILE);
        DrawHelper::Instance().drawStr2D(text, fontSize, padding, padding, textColor[Active], font);
    glEndList();
}

void GUITextButton::onReleased(Uint8 mouseState)
{
    // Defer to onReleased() as there is no mouse-specific handling to be done
    onReleased();
}

void GUITextButton::onReleased()
{
    // Go through all of the listeners in the list
    // and use the Call method of the Callback to fire
    // each method.
    for (unsigned int i = 0; i < callbacks.size(); i++)
    {
        // Notice, that we pass along a reference to THIS object
        // so that the listener can decipher which button was
        // clicked.  Imagine that the Call method is actually
        // any method that was passed as a callback. In this
        // example, it would be the MainMenu::onClicked method.
        // So, the program flow from here would take us to
        // that method, and whatever is within that method will
        // be executed.
        callbacks[i]->call(this);
    }
}

void GUITextButton::addCallback(Callback* cb)
{
    callbacks.push_back(cb);
}
